package org.zcl.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.zcl.utils.QueryService;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * @Package org.zcl.customer
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2021/12/3 13:38
 */
@Slf4j
@Service
public class QueryMainServiceImpl implements QueryService {

    private ExecutorService executorService;

    @PostConstruct
    public void init() {
        executorService = Executors.newCachedThreadPool();
    }

    public List<String> query() throws Exception {
        List<String> list = new ArrayList<>();
        long start = System.currentTimeMillis();
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
            list.add("频道1");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("module接口耗时统计：频道模块耗时:{}", System.currentTimeMillis() - start);
        List<Consumer> consumers = new ArrayList<>();
        consumers.add(unused -> {
            long start1 = System.currentTimeMillis();
            //拼接6大模块的数据，组装成大json
            //轮播图   ShopBannerVo
            try {
                TimeUnit.MILLISECONDS.sleep(5000);
                list.add("轮播图1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("module接口耗时统计：轮播图模块耗时:{}", System.currentTimeMillis() - start1);

        });
        consumers.add(unused -> {
            long start1 = System.currentTimeMillis();
            //金刚区   ShopChannelVo
            try {
                TimeUnit.MILLISECONDS.sleep(3000);
                list.add("金刚区1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("module接口耗时统计：金刚区模块耗时:{}", System.currentTimeMillis() - start1);
        });
        consumers.add(unused -> {
            long start1 = System.currentTimeMillis();
            //推荐类目 ShopHotChannelVO
            try {
                TimeUnit.MILLISECONDS.sleep(10000);
                list.add("推荐类目1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("module接口耗时统计：推荐类目模块耗时:{}", System.currentTimeMillis() - start1);

        });
        CountDownLatch countDownLatch = new CountDownLatch(consumers.size());
        consumers.forEach(consumer -> this.submit(countDownLatch, consumer));
        //等待3秒，3秒没结束就返回
        countDownLatch.await(3, TimeUnit.SECONDS);
        log.info("module接口耗时统计：{}", System.currentTimeMillis() - start);
        return list;
    }

    protected void submit(CountDownLatch countDownLatch, Consumer consumer) {
        executorService.submit(() -> {
            try {
                consumer.accept(null);
            } finally {
                countDownLatch.countDown();
            }
        });
    }
}
